package GUI;

import java.io.IOException;

import v3.Sauvegarde1;

import Kernel.Administrateur;

public class Administration extends javax.swing.JFrame {

	public static Administrateur admin;
	private static final long serialVersionUID = -281000400046223073L;

	/** Cree une nouvelle instance de Administration */
	public Administration() {
		initComponents();
		this.jTextArea1.setEditable(false);
		Administration.admin = new Administrateur();
		this.setLocationRelativeTo(this.getParent());
	}

	/**
	 * Cree une nouvelle instance parametree de Administration
	 * @param administrateur est l'administrateur
	 */
	public Administration(Administrateur administrateur) {
		initComponents();
		this.jTextArea1.setEditable(false);
		Administration.admin = administrateur;
		this.jTextArea1.setText(Administration.admin.getServeur().toStringLogs());
		this.setLocationRelativeTo(this.getParent());
	}

	/**
	 * Initialisation des composants d'AWT.
	 */	
	private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        quitter = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        deconnexion = new javax.swing.JMenuItem();
        quit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        consulterVehicule = new javax.swing.JMenuItem();
        ajouterVehicule = new javax.swing.JMenuItem();
        editerVehicule = new javax.swing.JMenuItem();
        supprimerVehicule = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        consulterTrajet = new javax.swing.JMenuItem();
        ajouterTrajet = new javax.swing.JMenuItem();
        editerTrajet = new javax.swing.JMenuItem();
        supprimerTrajet = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        consulterVille = new javax.swing.JMenuItem();
        ajouterVille = new javax.swing.JMenuItem();
        editerVille = new javax.swing.JMenuItem();
        supprimerVille = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        sauvegarder = new javax.swing.JMenuItem();
        charger = new javax.swing.JMenuItem();
        contenuFichier = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Panneau d'administration");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel2.setText("Dernieres actions effectuees :");

        quitter.setText("Quitter");
        quitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(33, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(391, Short.MAX_VALUE)
                .addComponent(quitter)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(41, 41, 41)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addComponent(quitter)
                .addContainerGap())
        );

        jMenu1.setText("File");

        deconnexion.setText("Se déconnecter");
        deconnexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deconnexionActionPerformed(evt);
            }
        });
        jMenu1.add(deconnexion);

        quit.setText("Quitter");
        quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitActionPerformed(evt);
            }
        });
        jMenu1.add(quit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Administration");

        jMenu3.setText("Vehicules");

        consulterVehicule.setText("Consulter les vehicules");
        consulterVehicule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consulterVehiculeActionPerformed(evt);
            }
        });
        jMenu3.add(consulterVehicule);

        ajouterVehicule.setText("Ajouter un vehicule");
        ajouterVehicule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ajouterVehiculeActionPerformed(evt);
            }
        });
        jMenu3.add(ajouterVehicule);

        editerVehicule.setText("Editer un vehicule");
        editerVehicule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editerVehiculeActionPerformed(evt);
            }
        });
        jMenu3.add(editerVehicule);

        supprimerVehicule.setText("Supprimer un vehicule");
        supprimerVehicule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supprimerVehiculeActionPerformed(evt);
            }
        });
        jMenu3.add(supprimerVehicule);

        jMenu2.add(jMenu3);

        jMenu4.setText("Trajets");

        consulterTrajet.setText("Consulter les trajets");
        consulterTrajet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consulterTrajetActionPerformed(evt);
            }
        });
        jMenu4.add(consulterTrajet);

        ajouterTrajet.setText("Ajouter un trajet");
        ajouterTrajet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ajouterTrajetActionPerformed(evt);
            }
        });
        jMenu4.add(ajouterTrajet);

        editerTrajet.setText("Editer un trajet");
        editerTrajet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editerTrajetActionPerformed(evt);
            }
        });
        jMenu4.add(editerTrajet);

        supprimerTrajet.setText("Supprimer un trajet");
        supprimerTrajet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supprimerTrajetActionPerformed(evt);
            }
        });
        jMenu4.add(supprimerTrajet);

        jMenu2.add(jMenu4);

        jMenu5.setText("Villes");

        consulterVille.setText("Consulter les villes");
        consulterVille.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consulterVilleActionPerformed(evt);
            }
        });
        jMenu5.add(consulterVille);

        ajouterVille.setText("Ajouter une ville");
        ajouterVille.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ajouterVilleActionPerformed(evt);
            }
        });
        jMenu5.add(ajouterVille);

        editerVille.setText("Editer une ville");
        editerVille.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editerVilleActionPerformed(evt);
            }
        });
        jMenu5.add(editerVille);

        supprimerVille.setText("Supprimer une ville");
        supprimerVille.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supprimerVilleActionPerformed(evt);
            }
        });
        jMenu5.add(supprimerVille);

        jMenu2.add(jMenu5);

        jMenuBar1.add(jMenu2);

        jMenu6.setText("Données");

        sauvegarder.setText("Sauvegarder");
        sauvegarder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					sauvegarderActionPerformed(evt);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        jMenu6.add(sauvegarder);

        charger.setText("Restaurer");
        charger.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					chargerActionPerformed(evt);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        jMenu6.add(charger);

        contenuFichier.setText("Voir le contenu des fichiers");
        contenuFichier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					contenuFichierActionPerformed(evt);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        jMenu6.add(contenuFichier);

        jMenuBar1.add(jMenu6);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }

	private void quitterActionPerformed(java.awt.event.ActionEvent evt) {
		this.setVisible(true);
		this.dispose();
	}

	private void quitActionPerformed(java.awt.event.ActionEvent evt) {
		this.setVisible(false);
		this.dispose();
	}

	private void consulterVehiculeActionPerformed(java.awt.event.ActionEvent evt) {
		ConsulterVehicule cv = new ConsulterVehicule();
		cv.setVisible(true);
		this.dispose();
	}

	private void deconnexionActionPerformed(java.awt.event.ActionEvent evt) {
		Accueil ac = new Accueil();
		this.setVisible(false);
		ac.setVisible(true);
		this.dispose();
	}

	private void ajouterVehiculeActionPerformed(java.awt.event.ActionEvent evt) {
		AjoutVehicule av = new AjoutVehicule();
		av.setVisible(true);
		this.dispose();
	}

	private void consulterTrajetActionPerformed(java.awt.event.ActionEvent evt) {
		ConsulterTrajet ct = new ConsulterTrajet();
		ct.setVisible(true);
		this.dispose();
	}

	private void ajouterTrajetActionPerformed(java.awt.event.ActionEvent evt) {
		AjoutTrajet at = new AjoutTrajet();
		at.setVisible(true);
		//this.dispose();
	}

	private void consulterVilleActionPerformed(java.awt.event.ActionEvent evt) {
		ConsulterVille cv = new ConsulterVille();
		cv.setVisible(true);
		this.dispose();
	}

	private void ajouterVilleActionPerformed(java.awt.event.ActionEvent evt) {
		AjoutVille av = new AjoutVille();
		av.setVisible(true);
		this.dispose();
	}

    private void sauvegarderActionPerformed(java.awt.event.ActionEvent evt) throws IOException, ClassNotFoundException {
    	if(Accueil.versionUtilisation.compareTo("V3") == 0){
    		try {
                Sauvegarde1.encodeToFile(Accueil.serveur,"Sauvegarde-Serveur.xml");
                System.out.println(Accueil.versionUtilisation);
               
            } catch(Exception e) {
                e.printStackTrace();
            }
    	}
    	else {
    		System.out.println(Accueil.versionUtilisation);
        	Accueil.serveur.sauvegarder(Accueil.versionUtilisation);
    	}   	
    }

    private void chargerActionPerformed(java.awt.event.ActionEvent evt) throws IOException, ClassNotFoundException {
    	if(Accueil.versionUtilisation.compareTo("V3") == 0){
    		 try {

 			 	//Accueil.serveur = (Serveur) (XMLTools.decodeFromFile("Sauvegarde-Serveur.xml"));
    			Accueil.serveur = Sauvegarde1.decodeFromFile("Sauvegarde-Serveur.xml");
 	           
 	        } catch(Exception e) {
 	            e.printStackTrace();
 	        }
    	}
    	else {
    		Accueil.serveur = Accueil.serveur.charger(Accueil.versionUtilisation);
    		Administration at = new Administration(admin);
        	at.setVisible(true);
        	this.setVisible(false);
        	this.dispose();
    	}
    	
    }
    
    private void contenuFichierActionPerformed(java.awt.event.ActionEvent evt) throws ClassNotFoundException, IOException {
    	ContenuFichier cf = new ContenuFichier();
    	cf.setVisible(true);
    }

    private void supprimerVehiculeActionPerformed(java.awt.event.ActionEvent evt) {
    	SupprimerVehicule sv = new SupprimerVehicule();
		sv.setVisible(true);
		this.dispose();
    }

    private void supprimerTrajetActionPerformed(java.awt.event.ActionEvent evt) {
    	SupprimerTrajet st = new SupprimerTrajet();
		st.setVisible(true);
		this.dispose();
    }

    private void supprimerVilleActionPerformed(java.awt.event.ActionEvent evt) {
    	SupprimerVille sv = new SupprimerVille();
		sv.setVisible(true);
		this.dispose();
    }
    
    private void editerVehiculeActionPerformed(java.awt.event.ActionEvent evt) {
        EditerVehicule ev = new EditerVehicule();
        ev.setVisible(true);
        this.dispose();
    }

    private void editerTrajetActionPerformed(java.awt.event.ActionEvent evt) {
    	 EditerTrajet et = new EditerTrajet();
         et.setVisible(true);
         this.dispose();
    }

    private void editerVilleActionPerformed(java.awt.event.ActionEvent evt) {
    	EditerVille ev = new EditerVille();
        ev.setVisible(true);
        this.dispose();
    }
 
    private javax.swing.JMenuItem ajouterTrajet;
    private javax.swing.JMenuItem ajouterVehicule;
    private javax.swing.JMenuItem ajouterVille;
    private javax.swing.JMenuItem charger;
    private javax.swing.JMenuItem consulterTrajet;
    private javax.swing.JMenuItem consulterVehicule;
    private javax.swing.JMenuItem consulterVille;
    private javax.swing.JMenuItem contenuFichier;
    private javax.swing.JMenuItem deconnexion;
    private javax.swing.JMenuItem editerTrajet;
    private javax.swing.JMenuItem editerVehicule;
    private javax.swing.JMenuItem editerVille;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JMenuItem quit;
    private javax.swing.JButton quitter;
    private javax.swing.JMenuItem sauvegarder;
    private javax.swing.JMenuItem supprimerTrajet;
    private javax.swing.JMenuItem supprimerVehicule;
    private javax.swing.JMenuItem supprimerVille;
    
}
